# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# 猫眼电影实时票房
# scrapy crawl maoyan_board
class MaoyanBoardSpider(scrapy.Spider):
    name = 'maoyan_board'

    allowed_domains = ['maoyan.com']

    start_urls = ['https://maoyan.com/']

    custom_settings = {
        'ITEM_PIPELINES': {
            'messAround.pipeline.maoyan.MaoyanBoardPipeline': 300
        }
    }

    def start_requests(self):
        url = 'https://piaofang.maoyan.com/getBoxList?date=1&isSplit=true'
        # url = 'https://piaofang.maoyan.com/dashboard-ajax?orderType=0'
        yield scrapy.Request(url=url, headers=util.default_headers, callback=self.parse)

    # 票房
    def parse(self, response):
        res = util.make_json(response.text)

        for key, val in enumerate(res['boxOffice']['data']['list']):
            yield {
                'no': key + 1,
                'name': val['movieInfo']['movieName'],
                # 上映天数
                'release': val['movieInfo']['releaseInfo'].replace("上映", '').replace("天", ''),
                # 综合票房（万元）
                'money': val['boxDesc'],
                # 综合票房占比
                'count_rate': val['boxRate'],
                # 排座占比
                'seat_count_rate': val['seatCountRate'],
                # 拍片占比
                'show_count_rate': val['showCountRate'],
            }
        pass
